package com.application.magazine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.aphidmobile.utils.AphidLog;
import com.aphidmobile.utils.UI;
import com.roscopeco.ormdroid.Entity;

public class BookFlipAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private int repeatCount = 1;
	private Context context;
	private List<Images> imgList;

	public BookFlipAdapter(Context context) {
		inflater = LayoutInflater.from(context);
		setInflater(inflater);
		this.context = context;
		Integer id = null;
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		id = preferences.getInt("mag_id",0);
		List<Images> list = Entity.query(Images.class).executeMulti();
		Magazine mag = Entity.query(Magazine.class).where("id").eq(id).execute();	
		imgList =  mag.images();
		System.gc();
	}

	@Override
	public int getCount() {
		return imgList.size();
	}

	public int getRepeatCount() {
		return repeatCount;
	}

	public void setRepeatCount(int repeatCount) {
		this.repeatCount = repeatCount;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View layout = convertView;
		
		if (convertView == null) {
			layout = inflater.inflate(R.layout.complex1, null);
			AphidLog.d("created new view from adapter: %d", position);
		}
		String filePath = imgList.get(position).path;
        File file = new File(filePath);
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inTempStorage = new byte[24*1024];

		Bitmap bitmapImage = decodeFile(file);
//		Matrix matrix = new Matrix();
//		matrix.postRotate(90);
//		if(bitmapImage.isRecycled()){
//			bitmapImage.isRecycled();
//		}
//		Bitmap rotated = Bitmap.createBitmap(bitmapImage,0,0,bitmapImage.getWidth(),bitmapImage.getHeight(),matrix,true);
		UI.<ImageView> findViewById(layout, R.id.photo).setImageBitmap(bitmapImage);

		
		System.gc();
		
		return layout;
	}
	private Bitmap decodeFile(File f){
	    try {
	        //Decode image size
	        BitmapFactory.Options o = new BitmapFactory.Options();
	        o.inJustDecodeBounds = true;
	        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

	        //The new size we want to scale to
	        final int REQUIRED_SIZE=300;

	        //Find the correct scale value. It should be the power of 2.
	        int scale=1;
	        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
	            scale*=2;

	        //Decode with inSampleSize
	        BitmapFactory.Options o2 = new BitmapFactory.Options();
	        o2.inSampleSize=scale;
	        
	        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
	    } catch (FileNotFoundException e) {
	    	e.printStackTrace();
	    }
	    return null;
	}

	public LayoutInflater getInflater() {
		return inflater;
	}

	public void setInflater(LayoutInflater inflater) {
		this.inflater = inflater;
	}
	
	
}
